package com.yanqu.road.dao.impl.activity.worldcup;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerData;
import com.yanqu.road.entity.activity.climbtower.UserClimbTowerEvent;
import com.yanqu.road.entity.activity.worldcup.UserWorldCupData;
import com.yanqu.road.entity.activity.worldcup.WorldCupSkill;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

public class UserWorldCupDataDaoImpl extends TempDataObjectDao<UserWorldCupData> {

    public static String getUserWorldCupDataSql = "select * from `t_u_worldcup` where `activityId`=? and `userId`=?";
    public UserWorldCupData getUserWorldCupData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWorldCupDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String insertUserWorldCupDataSql = "insert into `t_u_worldcup`(activityId,userId,skillMap,score,recruitAmount) values (?,?,?,?,?)";

    @Override
    protected boolean addData(UserWorldCupData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getSkillMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRecruitAmount()));
        if (getDb().executeNoneQuery(insertUserWorldCupDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateUserWorldCupDataSql = "update `t_u_worldcup` set skillMap=?,score=?,recruitAmount=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(UserWorldCupData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(data.getSkillMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRecruitAmount()));

        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateUserWorldCupDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected UserWorldCupData parseData(ResultSet rs) throws SQLException {
        UserWorldCupData data = new UserWorldCupData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setSkillMap(JSON.parseObject(rs.getString("skillMap"), new TypeReference<Map<Integer, WorldCupSkill>>() {}));
        data.setScore(rs.getInt("score"));
        data.setRecruitAmount(rs.getInt("recruitAmount"));
        return data;
    }
}
